package View.User;

import Service.ServiceImpl.UserServiceImpl;
import Service.UserService;
import View.Index.Index_admin;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddUser extends JFrame {


    public AddUser(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("添加用户");
        this.setSize(491,398);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("用户名:");
        label1.setBounds(71,27,65,30);
        JLabel label2= new JLabel();
        label2.setText("密码:");
        label2.setBounds(69,70,65,30);
        JLabel label3= new JLabel();
        label3.setText("用户类型:");
        label3.setBounds(68,118,65,30);
        JLabel label4= new JLabel();
        label4.setText("学号:");
        label4.setBounds(68,164,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(144,26,193,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(144,70,193,30);
        String[] item = new String[3];
        item[0]="请选择用户类型";
        item[1]="user";
        item[2]="admin";
        JComboBox comboBox1= new JComboBox(item);
        comboBox1.setBounds(143,118,196,30);
        JTextField field4= new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(145,161,190,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("添加");
        button1.setBounds(74,241,95,27);
        JButton button2= new JButton();
        button2.setText("取消");
        button2.setBounds(240,242,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(field1);
        this.add(field2);
        this.add(comboBox1);
        this.add(field4);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = field1.getText();
                String password = field2.getText();
                String userType = (String) comboBox1.getSelectedItem();
                String stunum = field4.getText();

                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setType(userType);
                user.setStunum(stunum);

                UserService service = new UserServiceImpl();
                service.addUser(user);

                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame,"添加成功!");
                field1.setText("");
                field2.setText("");
                field4.setText("");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Index_admin(user).setVisible(true);
                dispose();
            }
        });
    }
}
